﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Quality.BasicData;
using System.Data.SqlClient;

namespace Quality.BasicData
{
    public partial class assigneditems : Form
    {
        public assigneditems()
        {
            InitializeComponent();
            DB.getcombobox(typecomboBox, "testtypes", "num", "typename");
        }

        private void testtype_SelectedIndexChanged(object sender, EventArgs e)
        {
            test();
            
        }
        void test() {

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand();

            da.SelectCommand.CommandText = "select * from testitems where num not in (select itemnum from assigneditems where typenum='" + typecomboBox.SelectedValue + "')";

            DataSet dt1 = DB.getlist(da);
            listBox1.DataSource = dt1.Tables[0];
            listBox1.DisplayMember = "itemname";
            listBox1.ValueMember = "num";
            listBox1.SelectedIndex = -1;

            da.SelectCommand.CommandText = "select num,itemname from testitems where num  in (select itemnum from assigneditems where typenum='" + typecomboBox.SelectedValue + "')";

            DataSet dt2 = DB.getlist(da);
            listBox2.DataSource = dt2.Tables[0];
            listBox2.DisplayMember = "itemname";
            listBox2.ValueMember = "num";
            listBox2.SelectedIndex = -1;
        
        
        
        }
        private void input_Click(object sender, EventArgs e)
        {

            SqlCommand comm = new SqlCommand();
            if (listBox1.SelectedIndex!=-1&&typecomboBox.SelectedIndex!=-1)
            {

                foreach (DataRowView row in listBox1.SelectedItems)
                {
                    comm.CommandText = "insert into assigneditems(typenum,itemnum)values(@typenum,@itemnum)";
                    comm.Parameters.Clear();
                    comm.Parameters.AddWithValue("@typenum", typecomboBox.SelectedValue);
                    comm.Parameters.AddWithValue("@itemnum", row["num"]);

                    DB.dosql(comm);
                 

                }
                test();
            
              }
        }

        private void output_Click(object sender, EventArgs e)
        {
            SqlCommand comm = new SqlCommand();
            if (listBox2.SelectedIndex != -1 && typecomboBox.SelectedIndex != -1)
            {

                foreach (DataRowView row in listBox2.SelectedItems)
                {
                    comm.CommandText = "delete assigneditems where typenum=@typenum and itemnum=@itemnum";
                    comm.Parameters.Clear();
                    comm.Parameters.AddWithValue("@typenum", typecomboBox.SelectedValue);
                    comm.Parameters.AddWithValue("@itemnum", row["num"]);

                    DB.dosql(comm);
                  

                }
                test();

            }
        }
    }
}
